<?php
/**
 * 前台公共函数库
 * @Author SuperMan
 */

/**
 * @param $user_id
 * 获取用户头像
 */
function GetAvatar($user_id){
    $user = \app\common\model\User::where(['id'=>$user_id])->field(['avatar'])->find();
    if($user['avatar']){
        return  $user['avatar'];
    }else{
        return '__STATIC__/images/default_head.png';
    }
}

/**
 * 获取文章信息
 */
function GettopicInfo($id,$filed='*'){
    $topic =   \app\common\model\Article::where(['id'=>$id])->field($filed)->find();
    return $topic;
}
/**
 * 获取用户信息
 */
function GetMemberInfo($user_id,$filed='*'){
    $user = \app\common\model\User::where(['id'=>$user_id])->field($filed)->find();
    return $user;
}

/**
 * 时间转换成 几秒前 几分钟前 几小时前 几天前
 */
function time_tran($the_time){
    $now_time = time();
    $dur = $now_time - $the_time;
    if($dur < 0){
        return $the_time;
    }else{
        if($dur < 60){
            return $dur.'秒前';
        }else{
            if($dur < 3600){
                return floor($dur/60).'分钟前';
            }else{
                if($dur < 86400){
                    return floor($dur/3600).'小时前';
                }else{
                    if($dur < 259200){ //3天内
                        return floor($dur/86400).'天前';
                    }else{
                        return date('Y/m/d',$the_time);
                    }
                }
            }
        }
    }
}

/**
 * 单号生成 充值单号 体现单号 支付单号
 */
function numbers(){
    $time = date("YmdHis",time());
    $random = rand(1000,9999);
    $res = $time.$random;
    return $res;
}

/**
 * 时间戳转成年月日 时分秒
 */
function unixToDate($time){
    return date('Y-m-d H:i:s',$time);
}



/* 聚合短信请求接口返回内容
* @param  string $url [请求的URL地址]
* @param  string $params [请求的参数]
* @param  int $ipost [是否采用POST形式]
* @return  string
*/
function juhecurl($url,$params=false,$ispost=0){
    $httpInfo = array();
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}


function getJson($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}


function curl_get($url){
    //初始化
    $ch = curl_init();
    //设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  // 从证书中检查SSL加密算法是否存在
    //执行并获取HTML文档内容
    $output = curl_exec($ch);
    //释放curl句柄
    curl_close($ch);
    return $output;
}


